<?php

/**
 * WZ actions.
 *
 * @package    ProdAssistant
 * @subpackage WZ
 * @author     Paweł Świtalla
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class WZActions extends sfActions
{
  private $core;
  public function preExecute()
  {
    $this->core = PACore::getInstance();
  }
  
  public function executeCreate(sfWebRequest $request)
  {
    $url = $this->core->generateAppUrl('Store', 'storeWzHomepage');
    $this->form = new WZForm();
    if($request->isMethod('post'))
    {
      $params = $request->getParameter('wz');
      $this->form->bind($params);
      if($this->form->isValid())
      {
        $wz = $this->form->getObject();
        $wz->setUser($this->getUser()->getAttribute('userObject'));
        $wz = $this->form->save();

        // TODO: Fazy projektów w nazwie dokumentu
        $wz->setNumber("WZ-".$wz->getProject()->getShortcut()."-".date("Y-m-d", strtotime($wz->getCreatedAt()))."-".$wz->getId());
        $wz->updateWzItems($params['items_list_values']);
        $wz->save();

        if($request->isXmlHttpRequest())
        {
          $this->getResponse()->setContent('location@'.$url."#");
          return sfView::NONE;
        }
        else $this->redirect($url);
      }
    }
  }
  
  public function executeDelete(sfWebRequest $request)
  {
    $wz = Doctrine_Core::getTable('WZ')->find($request->getParameter('id'));
    if($wz instanceof WZ)
    {
      $wz->delete();
    }
    $this->redirect($request->getReferer());
  }

  public function executeUpdate(sfWebRequest $request)
  {
  
  }
  
  public function executeShow(sfWebRequest $request)
  {
    if(!$request->hasParameter('itemId'))
    {
      $this->wz = Doctrine_Core::getTable('WZ')->find($request->getParameter('id'));
      $wz = Doctrine_Query::create()
              ->select('w.id, wi.value, i.name, c.quantityUnit')
              ->from('WZ w')
              ->leftJoin('w.WZItems wi')
              ->leftJoin('wi.StoreItem i')
              ->leftJoin('i.Category c')
              ->where('w.id = ?', $request->getParameter('id'))
              ->execute()
              ->toArray();
    }
    else
    {
      $this->wz = WZTable::getLastWithItem($request->getParameter('itemId'));
      $wz = array();
      if($this->wz instanceof WZ)
      {
        $wz = Doctrine_Query::create()
                ->select('w.id, wi.value, i.name, c.quantityUnit')
                ->from('WZ w')
                ->leftJoin('w.WZItems wi')
                ->leftJoin('wi.StoreItem i')
                ->leftJoin('i.Category c')
                ->where('w.id = ?', $this->wz->getId())
                ->execute()
                ->toArray();
      }
    }
      
    if(count($wz) > 0)
    {
      $this->items = array();
      foreach($wz[0]['WZItems'] as $item)
      {
        $this->items[] = array('name' => $item['StoreItem']['name'], 'quantity' => $item['value'], 'quantityUnit' => $item['StoreItem']['Category']['quantityUnit'], 'annotation' => '');
      }
    }
        
    $this->setLayout(PACore::getLayoutsDir()."/document");
  }
  
  public function executeList(sfWebRequest $request)
  {
    if(!$request->hasParameter('itemId'))
    {
      $this->wzs = Doctrine_Core::getTable('WZ')->findAll();
    }
    else
    {
      $this->wzs = WZTable::getAllWithItem($request->getParameter('itemId'));
      $this->setLayout(PACore::getLayoutsDir().'/clear');
    }
  }
}
